using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Space
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class StateComponent : Microsoft.Xna.Framework.GameComponent, IState
    {
        GameState state = GameState.InGame;
        public GameState State { get { return state; } set { state = value; } }

        public StateComponent(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            Game.Services.AddService(typeof(IState), this);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        public void Reset()
        {
            state = GameState.InGame;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (Game.Services.GetService<IGameActions>().ProgramStage == Stage.Game)
            {
                IData data = Game.Services.GetService<IData>();

                if (data.SolarSystems.All(s => s.Planets.All(p => p.Player == data.SolarSystems.First().Planets[0].Player)))
                {
                    if (data.SolarSystems.First().Planets[0].Player == data.MainPlayer) state = GameState.Win;
                    else state = GameState.Lost;
                }
            }
            base.Update(gameTime);
        }
    }
}
